<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller as BaseController;
use App\Repository\Product\GoodsRemarkRepository;
use App\Repository\Product\GoodsRepository;
use Illuminate\Http\Request;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;

// 分销商
class GoodsController extends BaseController
{
    private $log;
    private $repoist;

    public function __construct(GoodsRepository $model)
    {
        $this->log = new Logger('goodsTwoEdit');
        $this->log->pushHandler(
            (new RotatingFileHandler(storage_path('/logs/goodsTwoEdit.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
        );
        $this->repoist = $model;
    }

    public function store(Request $request)
    {
        return $this->repoist->add($request);
    }

    public function index(Request $request)
    {
        $where = [];
        if (!empty($request->get('phone'))) {
            $where['where']['phone'] = $request->get('phone');
        }
        if (!empty($request->get('product_name'))) {
            $where['like'] = ['product_name' => $request->get('product_name')];
        }
        if (!empty($request->get('product_code'))) {
            $where['like'] = ['product_code' => $request->get('product_code')];
        }
        if (!empty($request->get('goods_code'))) {
            $where['like'] = ['goods_code' => $request->get('goods_code')];
        }
        if (!empty($request->get('name'))) {
            $where['like'] = ['name' => $request->get('name')];
        }
        if (($request->has('is_trun')) && $request->get('is_trun') != '') {
            $where['where']['is_trun'] = $request->get('is_trun');
        }
        if (($request->has('status')) && ($request->get('status') != '') && ($request->get('status') != -1)) {
            $where['where']['status'] = $request->get('status');
        }
        if (($request->has('smallStatus')) && ($request->get('smallStatus') != '') && ($request->get('smallStatus') != -1)) {
            $where['where']['is_app_all'] = $request->get('smallStatus');
        }
        if (($request->has('smallStatusWind')) && ($request->get('smallStatusWind') != '') && ($request->get('smallStatusWind') != -1)) {
            $where['where']['is_app'] = $request->get('smallStatusWind');
        }
        if ($request->has('orderByColumn') && !empty($request->get('orderByColumn')) && !empty($request->get('isAsc'))) {
            $ace = $request->get('isAsc') == 'descending' ? 'desc' : 'asc';
            $where['order'] = ['orderby' => [$request->get('orderByColumn') => $ace]];
        }
        $where['limit']['start'] = ($request->get('pageNum') - 1) * $request->get('pageSize');
        $where['limit']['number'] = $request->get('pageSize');
        if (empty($request->user->role_id) || $request->user->role_id != 1) {
            $where['where']['shop_id'] = $request->user->shop_id;
        }
        // var_dump($where);
        $data = $this->repoist->index($where);
        if ($data['code'] == 200 && $request->get('pageNum') == 1) {
            $data["operatorTypeList"] = config('card.operatorType');
        }
        return $data;
    }

    public function save(Request $request)
    {
        $params = $request->all();
        $this->log->info('商品修改修改参数:' . json_encode($params));
        if (!$request->get('id')) {
            return $this->returnMsg(1, '请传递id参数');
        }
        $where = ['id' => $request->get('id')];
        if (empty($request->user->role_id) || $request->user->role_id != 1) {
            $where['shop_id'] = $request->user->shop_id;
        }

        $res = $this->repoist->edit($where, $request);
        $this->log->info('商品修改修改参数:' . json_encode($params) . '返回参数:' . json_encode($res));
        return $res;
    }

    public function show(Request $request, $id)
    {
        $where = ['id' => $id];
        if (empty($request->user->role_id) || $request->user->role_id != 1) {
            $where['shop_id'] = $request->user->shop_id;
        }
        // var_dump($where);
        //查询一条数据
        $return = $this->repoist->getOne($where, ['id', 'name', 'product_id', 'xh_status',
            'oblong_pic',
            'detail_top_pic',
            'detail_bg_color', 'goods_point', 'passing_money', 'goods_price', 'goods_detail', 'goods_remark', 'main_pic', 'settle_require', 'goods_code', 'voice', 'flow', 'monthly', 'sort', 'aperator', 'is_service', 'tishi', 'is_trun', 'is_select_number','in_name']);
        if (!empty($return)) {
            // $return = usfirstArr($return);
            // 将文本模式 格式化
            if (!empty($return->goods_detail)) {
                $return->goods_detail = html_entity_decode($return->goods_detail);
            }
            if (!empty($return->goods_remark)) {
                $return->goods_remark = html_entity_decode($return->goods_remark);
            }
            if (!empty($return->is_select_number)) {
                $return->is_select_number = (int)$return->is_select_number;
            }
            if (isset($return->is_trun)) {
                $return->is_trun = (int)$return->is_trun;
            }
        }
        return $this->returnMsg(200, '查询成功', $return);
    }

    // 查询日志
    public function showLog(Request $request, $id)
    {
        $where = ['goods_id' => $id];
        if (empty($request->user->role_id) || $request->user->role_id != 1) {
            $where['shop_id'] = $request->user->shop_id;
        }
        $model = new GoodsRemarkRepository();
        $return = $model->getOne($where);
        return $this->returnMsg(200, '查询成功', $return);
    }

    public function listss(Request $request)
    {
        $where = [];
        if (!empty($request->get('name'))) {
            $where['where'] = ['name' => $request->get('name')];
        }
        $where['where']['shop_id'] = $request->user->shop_id;
        $where['where']['status'] = 0;
        // var_dump($where);
        if (empty($request->get('select'))) {
            $where['select'] = ['name', 'id'];
        }

        $return = $this->repoist->lists($where, 1, 99999);
        return $return;
    }

    public function destroy(Request $request, $id)
    {
        if (!$id) {
            return $this->returnMsg(1, '请传递参数');
        }
        $where = [
            'id' => $id,
            'shop_id' => $request->user->shop_id
        ];
        return $this->repoist->del($where);
    }

    public function updateStatus(Request $request, $id)
    {
        $where = [
            'id' => $id,
            //'shop_id' => $request->user->shop_id
        ];
        // 需要修改两个地方 一个是状态 一个 app是否上架
        $update = [
            'status' => $request->get('status'),
        ];
        $params = $request->all();
        $this->log->info('商品修改修改参数:' . json_encode($where) . '修改:' . json_encode($params));
        $res = $this->repoist->edit($where, $request);
        $this->log->info('商品修改修改参数:' . json_encode($params) . '返回结果:' . json_encode($res));
        return $res;
    }
}
